﻿using System;
using System.Collections.Generic;
using System.Text;
using EthernetTools.Utils;

namespace EthernetTools.DataBindinds
{
    public class String2ByteDataBinding : ConrolBinding
    {
        #region Constant And Fields

        private byte[] rawData;
        
        private ByteParseStringMethod vMethod = ByteParseStringMethod.String2ByteConverting;

        private System.EventHandler UpdateUICall ;

        private String stringValue;

        #endregion 
        
        #region Properties

        /// <summary>
        /// The byte[] data, null if not correct string present
        /// </summary>
        public override byte[] RawData
        {
            get
            {
                if (this.IsCorrect)
                {
                    return rawData;
                }

                return null;
            }
            set
            {
                this.StringValue = ByteUtils.ByteArrToString(value);
            }
        }

        /// <summary>
        /// How much must be data length
        /// </summary>
        public int CondDataLen
        {
            get;
            set;
        }

        public String StringValue
        {
            get
            {
                if (this.stringContainer != null)
                {
                    stringValue = stringContainer.Text;
                }

                return stringValue;
            }
            set
            {
                stringValue = value;
                if (this.stringContainer != null)
                {
                    stringContainer.Text = value;
                }
                Verify();
            }
        }
        #endregion
        
        #region Public Methods

        /// <summary>
        /// Binds instance to control
        /// </summary>
        public void Bind(System.Windows.Forms.Control stringContainer, 
            System.EventHandler UpdateUIFunction,
            ByteParseStringMethod vMethod = ByteParseStringMethod.String2ByteConverting, 
            int ConditionLength = -1,
            System.Windows.Forms.Control additionalStringDisplayer = null
            )
        {
            this.UpdateUICall = UpdateUIFunction;

            this.additionalStringDisplayer = additionalStringDisplayer;

            this.vMethod = vMethod;

            this.stringContainer = stringContainer;
            if (this.stringContainer != null)
            {
                this.stringContainer.Text = stringValue;

                this.stringContainer.TextChanged += new EventHandler(stringContainer_TextChanged);
            }

            this.CondDataLen = ConditionLength;

            base.Bind();
        }

        /// <summary>
        /// Return true if string successfully parsed and match all condition
        /// </summary>
        /// <returns></returns>
        public bool Verify()
        {
            string verifyingText = this.StringValue;
            if (stringContainer != null)
            {
                //string value in textbox
                
                if (stringContainer.Enabled)
                {
                    byte[] temp = new byte[0];
                    temp = ByteUtils.ParseBytes(verifyingText, vMethod);

                    bool pass = true;
                    if (this.CondDataLen > 0)
                    {
                        if (temp.Length != this.CondDataLen)
                        {
                            pass = false;
                        }
                    }

                    if (pass)
                    {
                        rawData = temp;
                    }
                    IsCorrect = pass;
                }
                else
                {
                    IsCorrect = true;
                }

                if (this.UpdateUICall != null)
                    this.UpdateUICall(this.stringContainer, null);
            }
            else
            {
                // just string value
                byte[] temp = new byte[0];
                temp = ByteUtils.ParseBytes(verifyingText, vMethod);

                bool pass = true;
                if (this.CondDataLen > 0)
                {
                    if (temp.Length != this.CondDataLen)
                    {
                        pass = false;
                    }
                }

                if (pass)
                {
                    rawData = temp;
                }
                IsCorrect = pass;
            }


            return IsCorrect;
           
        }

        public override void UnBind()
        {
            base.UnBind();
            this.UpdateUICall = null;

            if (this.stringContainer != null)
            {
                this.stringContainer.TextChanged -= stringContainer_TextChanged;
            }
        }
        #endregion
        
        #region Methods

        void stringContainer_TextChanged(object sender, EventArgs e)
        {
            Verify();
        }

        #endregion
    }
}
